package com.boomsecret.juc.collections;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author wangjinliang on 2019/1/23.
 */
public class SynchronousQueueDemo {
    private static SynchronousQueue<String> synchronousQueue = new SynchronousQueue<>();

    public static void main(String[] args) throws Exception {
        Thread threadA = new Thread(() -> {
            try {
                System.out.println("threadA start");
                synchronousQueue.put("aa");
                System.out.println("threadA end");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        Thread threadB = new Thread(() -> {
            try {
                System.out.println("threadB start");
                TimeUnit.SECONDS.sleep(2);
                System.out.println(synchronousQueue.poll());
                System.out.println("threadB end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        threadA.start();
        threadB.start();

        threadA.join();
        threadB.join();

        System.out.println("main end");
    }
}
